﻿using System.Text.RegularExpressions;
using System;
using System.Collections.Generic;

namespace XExcel.Validations
{
    public class MuiltValidate : Validate
    {
        private List<IValidate> items = new List<IValidate>();
        public IList<IValidate> Items => items.AsReadOnly();
        public void Add(IValidate validate)
        {
            items.Add(validate);
        }

        public override bool Test(object val)
        {
            foreach(var valid in items)
            {
                if(!valid.Test(val))
                {
                    Message = valid.Message;
                    return false;
                }
            }
            return true;
        }
        public MuiltValidate With(IValidate validate, string message = null)
        {
            items.Add(validate);
            return this;
        }

        public MuiltValidate WithRegex(string pattern, string message = null)
        {
            return With(Regex(pattern, message));
        }
        public MuiltValidate WithRegex(Regex regex, string message = null)
        {
            return With(Regex(regex, message));
        }
        public MuiltValidate WithDateRange(DateTime? datemin = null, DateTime? datemax = null, string message = null)
        {
            return With(DateRange(datemin, datemax, message));
        }
        public MuiltValidate WithNotNull(string message = null)
        {
            return With(NotNull());
        }
        public MuiltValidate WithFunction(Func<object, bool> valid, string message = null)
        {
            return With(Function(valid, message));
        }
    }
}
